﻿package net.wangit.adm.admin;

import net.wangit.adm.role.RoleTreeFactory;
import net.wangit.user.admin.Admin;
import net.wangit.user.admin.AdminFactory;
import net.wangit.util.Checker;
import net.wangit.view.tree.Tree;

public class AdminRoleTreeFactory extends RoleTreeFactory {

	private static final String CHECKBOX_UNCHECKED = "<input type=checkbox disabled/>";
	private static final String CHECKBOX_CHECKED = "<input type=checkbox disabled checked/>";
	private Admin administrator;

	public AdminRoleTreeFactory() {
	}

	public Tree createTree(String treeDoamin) throws Exception {
		String aid = AdminAuthUI.getAndSetAdministratorId();
		if (Checker.isEmpty(aid)) {
			return null;
		} else {
			administrator = AdminFactory.getAdmin(aid);
			Tree tree = super.createTree(treeDoamin);
			tree.setCheckAttribute("disabled");
			return tree;
		}
	}

	protected void setCustomAttribute(Tree tree, String roleId) {
		tree.setCheckValue(roleId);
		if (administrator != null && administrator.containsRole(roleId))
			tree.setCheckAttribute("checked");
		if (administrator != null && administrator.containsIndirectRole(roleId))
			tree.setAttribute("<input type=checkbox disabled checked/>");
		else
			tree.setAttribute("<input type=checkbox disabled/>");
	}

	protected void setRootAttribute(Tree tree1) {
	}

	protected void setName(Tree tree, String roleId, String roleName,
			int privilegeCount) {
		tree.setName((new StringBuilder(String.valueOf(privilegeCount)))
				.append(" ").append(roleName).toString());
	}
}
